package com.example.service;

import com.example.entity.InterfaceTrafficRecordDay;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class InterfaceTrafficRecordDayService {

    @Resource
    private SessionFactory sessionFactory;

    public void addInterfaceTrafficRecordDay(InterfaceTrafficRecordDay record) {
        try (Session session = sessionFactory.openSession()) {
            session.beginTransaction();
            session.save(record);
            session.getTransaction().commit();
        }
    }

    public InterfaceTrafficRecordDay getInterfaceTrafficRecordDayById(long id) {
        try (Session session = sessionFactory.openSession()) {
            return session.get(InterfaceTrafficRecordDay.class, id);
        }
    }

    public List<InterfaceTrafficRecordDay> getAllInterfaceTrafficRecordDays() {
        try (Session session = sessionFactory.openSession()) {
            return session.createQuery("from InterfaceTrafficRecordDay", InterfaceTrafficRecordDay.class).list();
        }
    }

    public void updateInterfaceTrafficRecordDay(InterfaceTrafficRecordDay record) {
        try (Session session = sessionFactory.openSession()) {
            session.beginTransaction();
            session.update(record);
            session.getTransaction().commit();
        }
    }

    public void deleteInterfaceTrafficRecordDay(long id) {
        try (Session session = sessionFactory.openSession()) {
            session.beginTransaction();
            InterfaceTrafficRecordDay record = session.get(InterfaceTrafficRecordDay.class, id);
            if (record != null) {
                session.delete(record);
            }
            session.getTransaction().commit();
        }
    }
}
